<?php
/*=============================================================================
|| ##################################################################
||	phpFoX Konsort
|| ##################################################################
||	
||	Copyright		: (C) 2005-2007 PHPFOX Limited a Reality Fox Creation
||	Contact			: info@phpfox.com
||
||	- phpFoX and all of its source code and files are protected by Copyright Laws. 
||
||	- The license for phpFoX permits you to install this software on a single domain (.com, .org, .net, etc.). 
||
||	- You may also not remove this copyright screen which shows the copyright information and credits for phpFoX (RealityFOX Creations). 
||
||	- phpFoX Konsort is NOT a FREE software - http://www.phpfox.com/license/
||
|| ##################################################################
=============================================================================*/

/* $File: ./include/modules/Event/classes/PhpFox_ComponentEventsList.class.php, $Author:  $ */



/** Component for listing events
 * @version $Id: PhpFox_ComponentEventsList.class.php 435 2008-04-18 15:06:49Z phpfoxsv $
 * @package module.event
 *   
 */
class PhpFox_Mod_Event_ComponentEventsList extends BaseComponent
{
    function process()
    {
        $oEventItem = $this->_oModule->getItem('Event');
        /* @var $oEventItem PhpFox_Mod_Event_ItemEvent */
        $oEventService = $this->_oModule->getService('Event');
        /* @var $oEventService PhpFox_Mod_Event_ServiceEvent */
        $oSecurityService = &App::getModuleService('Account', 'Security');
        /* @var $oSecurityService PhpFox_Mod_Account_ServiceSecurity */
        $oTpl = $this->_oModule->getTemplate();
        $oReq = &App::getRequest();
        $oUrl = &App::getUrl();
        App::loadClass('Filter');
        App::loadClass('Pager');

        $oSecurityService->checkPerm('events', array('read_only', 'full'));
        $oSecurityService->checkAccess('events');
        $sUser = $oSecurityService->getCurrentUserLogin();
        $iPage = $oReq->getInt('page', 1);
        $iPageSize = App::getParam('public.pagesize');
        $aEventItems = array();
        foreach (App::getTxtFile('event_items.txt') as $sItem)
        {
            $aEventItems[$sItem] = $sItem;
        }    
        $aCountries = array();
        $aCountries[''] = App::format('option.select');
        foreach (App::getTxtFile('country.txt') as $sItem)
        {
            $aCountries[$sItem] = $sItem;
        }
        $aStates = array();
        $aStates[''] = App::format('option.select');
        foreach (App::getTxtFile('us_stat.txt') as $sItem)
        {
            $aStates[$sItem] = $sItem;
        }
        $aFilter = array
        (
        	'type' => array
        	(
        		'value'  => '=',
        		'select' => $aEventItems,
        	),
        	'state' => array
        	(
        		'value'  => '=',
        		'select' => $aStates,
        	),
        	'country' => array
        	(
        		'value'  => '=',
        		'select' => $aCountries,
        	),
        	'zip' => '=',
        	'city' => 'like',
        	'title' => 'like',
        );
        $oFilter = &new Filter('browseevent', $aFilter);
        
		$oDb =& Database::get();
		$aOldEvents = $oDb->getRows("SELECT id FROM " . App::getT('events') . " WHERE private = 1 AND month IN(" . date('n', time()) . "," . date('m', time()) . ") AND day < '" . date('j', time()) . "' AND year = '" . date('Y', time()) . "'");
		$sIds = '';
		foreach($aOldEvents as $aOldEvent)
		{
			$sIds .= '' . $aOldEvent['id'] .',';
		}
		$sIds = rtrim($sIds, ',');

        $aCond = array
        (
        	" ((month >= '" . date('n', time()) . "' OR month >= '" . date('m', time()) . "') AND year >= '" . date('Y', time()) . "') "
        );
        
        if (count($aOldEvents))
        {
        	$aCond[] = " id NOT IN(" . $sIds . ")";
        }
      
        foreach ($oFilter->aConditions as $sKey => $sCond)
        {
            if ($sKey == 'title')
            {
                $sCondition = '('.str_replace('title', 'short', $sCond).' OR ';
                $sCondition .= str_replace('title', 'text', $sCond).' OR ';
                $sCondition .= $sCond.')';
                $aCond[] = $sCondition;
            }
            else
            {
                $aCond[] = $sCond;
            }
        }
        $sSort = 'year ASC, month ASC, day ASC, time DESC';
      	list($aItems, $iCnt) = $oEventService->getEventList($aCond, $iPage, $iPageSize, $sSort);
        foreach ($aItems as $iKey => $aItem)
    	{
    	    $aItems[$iKey]['time'] = mktime(0, 0, 0, $aItem['month'], $aItem['day'], $aItem['year']);
    	}
      	$oPager = &new Pager($iCnt, $iPage, $iPageSize);

      	$oTpl->assign(array(
      		'aFilter' => $oFilter->aHtml,
      		'aItems'  => $aItems,
      		'aPaging' => ($iCnt) ? $oPager->getInfo($oUrl) : '',
      		'bZipState'   => App::getSetting('us_states'),
      	));
      	return $oTpl->fetch('EventsList.html');
    }
}
?>